/*
 * @Description: 支付结果页
 * @Author: ziwei.ma
 * @Date: 2019-08-22 18:01:33
 * @Last Modified by: buwei.huangang
 * @Last Modified time: 2020-03-12 14:40:20
 */

<template>
  <div id="App">
    <payment-success
      v-if="paymentStatu==='1'"
      :payFromUrl="payFrom"
      :byFromType="byFromType"
    ></payment-success>
    <payment-failed
      v-else-if="paymentStatu==='0'"
      :payFromUrl="payFrom"
    ></payment-failed>
    <submit-success
      v-else-if="paymentStatu==='2'"
      :payFromUrl="payFrom"
    ></submit-success>
  </div>
</template>

<script>
import PaymentSuccess from './components/PaymentSuccess'
import PaymentFailed from './components/PaymentFailed'
import SubmitSuccess from './components/SubmitSuccess'
import { getStatusInquiry } from './api/api'
import { getUrlParamByKey } from 'vue-xiaobu-utils'
const PAYMENT_FAILED = '0' // 支付失败
const PAYMENT_SUCCESS = '1' // 支付成功
const SUBMIT_SUCCESS = '2' // 提交成功
export default {
  components: {
    'payment-success': PaymentSuccess,
    'payment-failed': PaymentFailed,
    'submit-success': SubmitSuccess
  },
  data() {
    return {
      paymentStatu: '', // 支付状态，1为成功，2为失败
      orderNo: '', // 预约单订单号
      orderStatu: '', // 订单状态
      index: '',
      byFromType: '', // 区分是包车还是定制班线
      payFrom: 'bcHomePage.html?_tv=false'// 结果页按钮跳转路径，默认为跳转至包车首页
    }
  },
  methods: {
    inquireOrder(orderNo) {
      getStatusInquiry(this.orderNo).then(res => {
        this.paymentStatu = SUBMIT_SUCCESS
        this.$UI.then(UI => {
          UI.setTitle('提交成功', () => { }, () => { })
        })
      }).catch(() => {
        this.index++
        if (this.index < 10) {
          setTimeout(() => {
            // 如果查询失败则调用自身再次查询，查询10次，间隔500毫秒
            this.inquireOrder()
          }, 500)
        }
      })
    },
    getPayFrom() {
      if (getUrlParamByKey('PAY_FROM')) {
        let payFromType = getUrlParamByKey('PAY_FROM')
        // 区分是包车还是定制班线
        this.byFromType = payFromType
        if (payFromType === 'TYPE_CUSTOM') {
          this.payFrom = 'customRouteOrderList.html?_tv=true&tagRefresh=true'
        } else if (payFromType === 'TYPE_BC') {
          this.payFrom = 'bcOrderList.html?_tv=true&tagRefresh=true'
        }
      } else {
        return this.payFrom
      }
    }
  },
  mounted() {
    this.getPayFrom()
    this.paymentStatu = getUrlParamByKey('paymentStatu')
    this.orderNo = getUrlParamByKey('orderNO')
    if (this.paymentStatu === PAYMENT_SUCCESS) {
      this.$UI.then(UI => {
        UI.setTitle('支付成功', () => { }, () => { })
      })
      if (!getUrlParamByKey('CR_orderNO')) {
        this.inquireOrder(this.orderNo)
      }
    } else if (this.paymentStatu === PAYMENT_FAILED) {
      this.$UI.then(UI => {
        UI.setTitle('支付失败', () => { }, () => { })
      })
    }
  }
}
</script>

<style>
html,
body,
#App {
  height: 100%;
}
</style>
